//
//  HomeImageViewController.m
//  110
//
//  Created by 沈鑫 on 16/4/8.
//  Copyright © 2016年 乔砥. All rights reserved.
//

#import "HomeImageViewController.h"
#import "HomeImageOrVitaminTableViewCell.h"
#import "RequestManager.h"
#import "HomeImageModel.h"
#import "UIImageView+WebCache.h"
#import "PharmacyViewController.h"
@interface HomeImageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UIImageView *myImageView;

@property (nonatomic, strong)NSMutableArray *myArray;

@property (nonatomic, strong)NSString *url;

@property (nonatomic, assign)NSInteger row;
@end
// 这个界面我要和维生素公用这个controller
@implementation HomeImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"活动详情";
    // navigation两边button 的颜色
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    // navigation字体的颜色 大写 型号
    [self.navigationController.navigationBar setTitleTextAttributes: @{NSFontAttributeName: [UIFont fontWithName:@"Arial-BoldItalicMT" size:19],
                                                                       
                                                                       
                                                                       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButton:)];
    leftButton.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // 注册
    [_tableView registerClass:[HomeImageOrVitaminTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 设置表头
    _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT / 3)];
    [self.tableView setTableHeaderView:_myImageView];
    
    [self request];
}

- (void)leftBarButton:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)request
{
    if (_isOneOrTow == 0)
    {
        _url = @"http://openapi.ypt.langma.cn/yws/?json=%7B%22op_type%22%3A%221006%22%2C%22uid%22%3A%220%22%2C%22c_ver%22%3A%2240000%22%2C%22activity_id%22%3A%221%22%2C%22c_type%22%3A%221%22%2C%22cid%22%3A%221%22%7D";
    }
    else
    {
        _url = @"http://openapi.ypt.langma.cn/yws/?json=%7B%0A%20%20%22op_type%22%20%3A%201006%2C%0A%20%20%22c_ver%22%20%3A%20%224.0.0%22%2C%0A%20%20%22c_type%22%20%3A%200%2C%0A%20%20%22uid%22%20%3A%200%2C%0A%20%20%22activity_id%22%20%3A%20%222%22%2C%0A%20%20%22cid%22%20%3A%200%0A%7D";
    }
    [RequestManager requestWithUrl:_url requestType:RequestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        _myArray = [HomeImageModel getData:dic];
        HomeImageModel *model = _myArray[0];
        [_myImageView sd_setImageWithURL:[NSURL URLWithString:model.banner_image_url]];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"药品界面网络错误");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myArray.count / 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeImageOrVitaminTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    // 取消cell点击响应
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // cellButton点击方法
    [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 因为复用问题 所以在这儿给tag值
    cell.button.tag = indexPath.row * 2 + 1;
    [cell.button1 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    // 因为复用所以在这里给tag值
    cell.button1.tag = indexPath.row * 2 + 2;
    HomeImageModel *model1 = _myArray[indexPath.row * 2 + 1 ];
    HomeImageModel *model2 = _myArray[indexPath.row * 2 + 2];
    [cell getModel:model1 modle2:model2];
    // 取消渲染
    cell.selectionStyle = UITableViewCellAccessoryNone;
//    NSLog(@"%ld",indexPath.row);
    return cell;
}

#pragma mark --- 第一列cell ---
- (void)buttonAction:(UIButton *)button
{
    PharmacyViewController *pharmacy = [[PharmacyViewController alloc]init];

    _row = button.tag;
    HomeImageModel *model = _myArray[_row];
    pharmacy.HomeImageModel = model;
    pharmacy.towBool = 1;
//    NSLog(@"%ld",_row);
//    NSLog(@"%@",pharmacy.HomeImageModel.ID);
    [self.navigationController pushViewController:pharmacy animated:YES];
    
}

#pragma mark --- 第二列cell ---
- (void)buttonAction1:(UIButton *)button
{
    PharmacyViewController *pharmacy = [[PharmacyViewController alloc]init];
    _row = button.tag;
    HomeImageModel *model = _myArray[_row];
    pharmacy.HomeImageModel = model;
    pharmacy.towBool = 1;
//    NSLog(@"%ld",button.tag);
    //    NSLog(@"%@",pharmacy.HomeImageModel.ID);
    //    NSLog(@"row===%ld",_row);
    [self.navigationController pushViewController:pharmacy animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
